<?php

/**
 * Object Cache API
 * 
 * @package CityBao
 * @subpackage Cache
 */

/**
 * 如果缓存名称不存在，添加数据到缓存
 *
 * @uses $cb_object_cache Object Cache Class
 * @see cb_Object_Cache::add()
 *
 * @param int|string $key 
 * @param mixed $data 
 * @param string $flag 缓存分组存放
 * @param int $expire 过期时间
 * @return unknown
 */
function cb_cache_add($key, $data, $flag = '', $expire = 0) {
    global $cb_object_cache;

    return $cb_object_cache->add($key, $data, $flag, $expire);
}

/**
 * 关闭缓存
 *
 * @return bool Always returns True
 */
function cb_cache_close() {
    return true;
}

/**
 * 删除指定缓存
 * 
 * @uses $cb_object_cache Object Cache Class
 * @see cb_Object_Cache::delete()
 *
 * @param int|string $id 
 * @param string $flag 
 * @return bool 
 */
function cb_cache_delete($id, $flag = '') {
    global $cb_object_cache;

    return $cb_object_cache->delete($id, $flag);
}

/**
 * 删除所有缓存
 *
 * @uses $cb_object_cache Object Cache Class
 * @see cb_Object_Cache::flush()
 *
 * @return bool Always returns true
 */
function cb_cache_flush() {
    global $cb_object_cache;

    return $cb_object_cache->flush();
}

/**
 * 根据ID和flag返回缓存内容
 * 
 * @uses $cb_object_cache Object Cache Class
 * @see cb_Object_Cache::get()
 *
 * @param int|string $id 
 * @param string $flag 
 * @return bool|mixed 
 */
function cb_cache_get($id, $flag = '') {
    global $cb_object_cache;
    $return = $cb_object_cache->get($id, $flag);
    if (!$return) {
        $return = cb_cache_replace_by_id($flag, $id);
    }
    return $return;
}

/**
 * 初始化缓存
 * 
 * @global cb_Object_Cache $cb_object_cache
 */
function cb_cache_init() {
    $GLOBALS['cb_object_cache'] = & new cb_Object_Cache();
}

/**
 * 更新指定缓存
 * 
 * @uses $cb_object_cache Object Cache Class
 * @see cb_Object_Cache::replace()
 *
 * @param int|string $id 
 * @param mixed $data 
 * @param string $flag 
 * @param int $expire 
 * @return bool 
 */
function cb_cache_update($key, $data, $flag = '', $expire = 0) {
    global $cb_object_cache;

    return $cb_object_cache->replace($key, $data, $flag, $expire);
}

/**
 * 更新指定ID缓存
 *
 * @uses $cb_object_cache Object Cache Class
 * @see cb_Object_Cache::replace()
 *
 * @param int|string $id
 * @param int $expire
 * @return mix
 */
function cb_cache_replace_by_id($flag, $key='', $expire = 0) {
    global $cb_object_cache, $cbdb;
    $cache_sql = include CB_INCLUDE . 'cache/cache_sql.inc.php';
    $current_sql = array();
    if ($key == '') {
        if (isset($cache_sql[$flag])) {
            $current_sql[$flag] = $cache_sql[$flag];
        }
    } else {
        if (isset($cache_sql[$flag][$key])) {
            $current_sql[$flag][$key] = $cache_sql[$flag][$key];
        }
    }
    $data = array();
    foreach ($current_sql as $flag => $value) {
        foreach ($value as $key => $sql) {
            $data_db = $cbdb->get_results($sql, ARRAY_A);
            if ($flag == 'modules') {
                foreach ($data_db as $temp) {
                    $data[$temp['moduleid']] = $temp;
                }
            } elseif ($flag == 'settings') {
                $data = normalizer_dbsettings($data_db);
            } else {
                $data = $data_db;
            }
            $cb_object_cache->replace($key, $data, $flag, $expire);
        }
    }
    return $data;
}

/**
 * 保存数据到缓存
 *
 * @since 2.0
 * @uses $cb_object_cache Object Cache Class
 * @see cb_Object_Cache::set()
 *
 * @param int|string $id 
 * @param mixed $data 
 * @param string $flag 
 * @param int $expire 
 * @return bool 
 */
function cb_cache_set($key, $data, $flag = '', $expire = 0) {
    global $cb_object_cache;

    return $cb_object_cache->set($key, $data, $flag, $expire);
}

/**
 * 刷新指定缓存
 * @param string $flag
 * @param string $key
 * @param string $expire
 * @return mix
 */
function cb_cache_refresh($flag, $key='', $expire = 0){
    return cb_cache_replace_by_id($flag, $key, $expire);
}